class Documents::ConferencesController < Documents::BaseController
  before_filter :already_member?, :only => [:show, :join, :leave, :members]
  before_filter :only_member_access, :only => [:show, :members]

  def index
    @search = Conference.search(params[:search])
    @conferences = @search.page(params[:page])

    @data = {}
    @conferences.each do |conference|
      @data[conference.id] = {}
      @data[conference.id][:status] = Conference::STATUSES[conference.status]
      @data[conference.id][:document] = conference.document
      @data[conference.id][:document_tags] = conference.document.tags
      @data[conference.id][:reports] = conference.reports
      @data[conference.id][:members_count] = conference.members.count
      @data[conference.id][:member] = conference.members.include? current_user
    end
  end

  def show
    @status        = Conference::STATUSES[@conference.status]
    @document      = @conference.document
    @comments      = @conference.document.comments
    @members       = @conference.get_paginated_members
    @members_count = @conference.members.count
    @reports       = @conference.reports
  end

  def members
    members = @conference.get_paginated_members params[:page], params[:remaining]

    respond_to do |format|
      format.json { render :json => @conference.members_to_json(members) }
    end
  end

  def new
    @conference = Conference.new
  end

  def create
    @conference = Conference.new(params[:conference])
    @conference.document.user = current_user

    if @conference.save
      @conference.members << current_user
      flash[:notice] = t('conferences.create.success')
      redirect_to documents_conferences_path
    else
      render :action => :new
    end
  end

  def update
    if @conference.update_attributes(params[:conference])
      flash[:notice] = t('conferences.update.success')
      redirect_to :action => :index
    else
      render :action => :edit
    end
  end

  def join
    unless @conference.closed?
      unless @member
        if @conference.members << current_user
          flash.now[:notice] = t('conferences.join.success')
          @member = @success = true
          @members = @conference.members.count
        end
      else
        flash.now[:alert] = t('conferences.join.error.already_member')
      end
    else
      flash.now[:alert] = t('conferences.join.error.closed')
    end

    respond_to do |format|
      format.html { redirect_to documents_conference_path(@conference) }
      format.js
    end
  end

  def leave
    unless @conference.closed?
      if @conference.members.include? current_user
        if @conference.members.delete current_user
          flash.now[:notice] = t('conferences.leave.success')
          @member, @members, @success = false, @conference.members.count, true
        end
      else
        flash.now[:alert] = t('conferences.leave.error.not_member')
      end
    else
      flash.now[:alert] = t('conferences.leave.error.closed')
    end

    respond_to do |format|
      format.html { redirect_to documents_conferences_path }
      format.js
    end
  end

  protected
    def already_member?
      @member = @conference.members.include? current_user
    end

    def only_member_access
      unless @member
        flash[:alert] = t('conferences.member_access_error')
        redirect_to documents_conferences_path
      end
    end
end
